package register;
import lombok.extern.log4j.Log4j;

import java.util.concurrent.*;

import static register.ServiceRegistry.zooKeeper;
@Log4j
public class ServiceHeartbeat {
    private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

    public static void startHeartbeat(String path, String data) {
        final Runnable heartbeat = () -> {
            try {
                if (zooKeeper.exists(path, false) != null) {
                    zooKeeper.setData(path, data.getBytes(), -1);
                    log.info("心跳更新: " + path);
                }
            } catch (Exception e) {
                log.error("心跳发送失败: " + e.getMessage());
            }
        };

        scheduler.scheduleAtFixedRate(heartbeat, 0, 10, TimeUnit.SECONDS);
    }
}

